<template>
  <div>
    <!-- 面包屑 -->
    <subBread></subBread>
    <!-- 筛选区 -->
    <SubFilter @filter-change="filterChange"></SubFilter>
    <!-- 商品面板（排序+列表 -->
    <div class="goods-list">
      <!-- 排序 -->
      <SubSort @sortChange="getSort"></SubSort>
      <!-- 列表 -->
      <ul>
        <li v-for="item in goodsList" :key="item">
          <goodItem :goods="item" />
        </li>
      </ul>
      <!-- 加载更多 -->
      <XtxInfiniteLoading
        :loading="loading"
        :finished="finished"
        @infinite="getData"
      ></XtxInfiniteLoading>
    </div>
  </div>
</template>

<script>
import subBread from './component/sub-bread.vue'
import SubFilter from './component/sub-filter.vue'
import SubSort from './component/sub-sort.vue'
import goodItem from '@/views/category/component/goods-item.vue'
import { findSubCategoryGoods } from '@/api/category.js'
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'

export default {
  components: {
    subBread,
    SubFilter,
    SubSort,
    goodItem
  },
  setup() {
    const route = useRoute()
    // 加载中
    const loading = ref(false)
    // 是否加载完毕
    const finished = ref(false)
    // 复选框的值
    const isAllCheched = ref(true)
    // 商品列表
    const goodsList = ref([])
    let reqParams = {
      page: 1,
      pageSize: 20
    }
    // 获取商品列表数据
    const getGoodsList = () => {
      reqParams.categoryId = route.params.id
      findSubCategoryGoods(reqParams).then(({ result }) => {
        // 获取数据成功
        if (result.items && result.items.length) {
          goodsList.value.push(...result.items)
          reqParams.page++
        } else {
          // 没有数据
          console.log(1)
          finished.value = true
        }
        loading.value = false
      })
    }
    const getData = () => {
      loading.value = true
      getGoodsList()
    }
    // 在跳转页面时商品列表重新更新
    watch(
      () => route.params.id,
      (newValue) => {
        if (newValue && `/category/sub/${newValue}` === route.path) {
          goodsList.value = []
          reqParams.page = 1
          finished.value = false
        }
      }
    )
    // 1.更改排序组件的赛选属性，重新请求
    const getSort = (val) => {
      // 合并请求参数，保留之前的参数
      reqParams = { ...reqParams, ...val }
      reqParams.page = 1
      goodsList.value = []
      getGoodsList()
    }
    // 2.筛选组件的赛选数据，重新请求
    const filterChange = (value) => {
      reqParams = { ...reqParams, ...value }
      reqParams.page = 1
      goodsList.value = []
      getGoodsList()
    }
    return {
      isAllCheched,
      getData,
      loading,
      finished,
      goodsList,
      getSort,
      filterChange
    }
  }
}
</script>

<style scoped lang="less">
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>
